package com.langsin.blog.dao.impl;

import com.langsin.blog.dao.ArticleDao;
import com.langsin.blog.domain.Article;
import com.langsin.blog.domain.ArticleContent;
import com.langsin.blog.domain.User;
import com.langsin.blog.util.HibernateUtils;
import org.hibernate.Session;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ArticleDaoImp implements ArticleDao{

    private Session session;
    private String hql;

    public boolean articleAdd(Article article) {
        session = HibernateUtils.getSession();
        try {
            session.beginTransaction();
            session.save(article);
            session.getTransaction().commit();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
            return false;
        }finally {
            HibernateUtils.closeSession(session);
        }
    }

    public boolean articleModify(int id, String prop, String value) {
        session = HibernateUtils.getSession();
        try {
            session.beginTransaction();
            Article article = session.load(Article.class,id);
            if(prop.equals("title")){
                article.setTitle(value);
            }else if(prop.equals("text")){
                article.getArticleContent().setText(value);
            }else{
                System.err.println("无此属性");
            }
            session.getTransaction().commit();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
            return false;
        }finally {
            HibernateUtils.closeSession(session);
        }
    }

    public boolean articleDel(int id) {
        session = HibernateUtils.getSession();
        try {
            session.beginTransaction();
            Article article = session.load(Article.class,id);
            session.delete(article);
            session.getTransaction().commit();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
            return false;
        }finally {
            HibernateUtils.closeSession(session);
        }
    }

    @Test
    public void test(){
        this.articleListGet(1);
    }

    public List<Article> articleListGet(int id) {
        session = HibernateUtils.getSession();
        try {
            session.beginTransaction();
            hql = "FROM Article article WHERE article.user.id = " + id;
            List<Article> articleList =  session.createQuery(hql).list();
            session.getTransaction().commit();
            return articleList;
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
            return null;
        }finally {
            HibernateUtils.closeSession(session);
        }
    }

    public Article articleGet(String articleId) {
        session = HibernateUtils.getSession();
        Article article = null;
        try {
            session.beginTransaction();
            hql = "From Article article WHERE article.id = " + articleId;
            article = (Article) session.createQuery(hql).getSingleResult();
            session.getTransaction().commit();
            return article;
        }catch (Exception e){
            e.printStackTrace();
            session.getTransaction().rollback();
            return null;
        }finally {
            HibernateUtils.closeSession(session);
        }
    }
}
